package com.lanhao.aot.carrierintegration.rupost;

import java.io.File;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;

public class NewRussianPostUtil {
	private static final Logger LOG = LoggerFactory.getLogger(NewRussianPostUtil.class);
	public static final String RETURN_ORDER_URL = "https://otpravka-api.pochta.ru/1.0/returns/return-without-direct";
	public static final String CLEAN_ADDRESS_URL = "https://otpravka.pochta.ru/1.0/clean/address";
	public static final String TOKEN = "O2MfUzNN1DGH5x0YJlsd6s4PsOuxNsjt";
	public static final String LOGIN = "sesenovich@gmail.com";
	public static final String PASSWORD = "Nbcnvn91";
	public static String AUTHORIZATION = Base64.encode(LOGIN + ":" + PASSWORD);
	
	public static void main(String[] args) {
		getBarCodePdfFile("80080190040525");
	}
	
	
	public void createOrder() {
		JSONArray requestArray = new JSONArray();
		JSONObject request = new JSONObject();
		
		JSONObject addressFrom = new JSONObject();
		addressFrom.put("address-type", "DEFAULT");
		addressFrom.put("house", "37");
		addressFrom.put("index", "117105");
		addressFrom.put("location", "MockBa");
		addressFrom.put("place", "MockBa");
		addressFrom.put("region", "MockBa");
		addressFrom.put("room", "24");
		addressFrom.put("street", "MockBa");
		request.put("address-from", addressFrom);
		
		request.put("insr-value", 10000);
		request.put("mail-type", "EASY_RETURN");
		request.put("order-num", "2345");
		request.put("postoffice-code", "193230");
		request.put("recipient-name", "Soft Cell");
		request.put("sender-name", "Bronski Beat");
		requestArray.add(request);
		
		System.out.println(requestArray.toString());
//		createReturnOrder(requestArray.toJSONString());
//		
//		JSONObject address = new JSONObject();
//		address.put("id", "SenderAddress");
//		address.put("original-address", "Moscow, Varshavskoe shosse 37");
//		getCleanAddress(address.toJSONString());
	}
	
	public static Map<String, String> getRequestHeader() {
		Map<String, String> headers = Maps.newHashMap();
		headers.put("Content-Type", "application/json");
		headers.put("Accept", "application/json;charset=UTF-8");
		headers.put("Authorization", "AccessToken " + TOKEN);
		headers.put("X-User-Authorization", "Basic " + AUTHORIZATION);
		return headers;
	}
	
	
	public static String createReturnOrder(String requestBody) {
		Map<String, String> headers = getRequestHeader();
		String respBody = HttpRequest.put(RETURN_ORDER_URL).addHeaders(headers).body(requestBody).execute().body();
		LOG.info("url={}, header={}, request={}, response={}", RETURN_ORDER_URL, headers, requestBody, respBody);
		return respBody;
	}
	
	public static String getCleanAddress(String requestBody) {
		Map<String, String> headers = getRequestHeader();
		String respBody = HttpRequest.post(CLEAN_ADDRESS_URL).addHeaders(headers).body(requestBody).execute().body();
		LOG.info("url={}, header={}, request={}, response={}", CLEAN_ADDRESS_URL, headers, requestBody, respBody);
		return respBody;
	}
	
	public static void getBarCodePdfFile(String barCode) {
		Map<String, String> authHeader = getRequestHeader();
		String barcodesPdfurl = "https://otpravka-api.pochta.ru/1.0/forms/{}/easy-return-pdf";
		String realPdfPrintUrl = StrUtil.format(barcodesPdfurl, barCode) + "?print_type=PAPER";
		File localDestFile = new File("D:/" + barCode + ".pdf");
		LOG.info("[保存面单至本地]开始调用面单保存至本地方法, url={}", realPdfPrintUrl);
		long writeByteLength = HttpRequest.get(realPdfPrintUrl).addHeaders(authHeader).timeout(30000).execute().writeBody(localDestFile);
		LOG.info("[保存面单至本地]将返回流信息写入文件，写入字节数：{}", writeByteLength);
	}
	
}
